package com.springboot.hiresystem.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.hiresystem.domain.JobSkillMappings;
import com.springboot.hiresystem.domain.JobSkills;
import com.springboot.hiresystem.domain.Jobs;
import com.springboot.hiresystem.dto.req.JobSkillMappingUpdateRequest;
import com.springboot.hiresystem.dto.resp.PageResponse;
import com.springboot.hiresystem.dto.resp.Result;
import com.springboot.hiresystem.service.JobSkillMappingsService;
import com.springboot.hiresystem.service.JobSkillsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/jobskillmappings")
@Slf4j
public class JobSkillMappingController {

    private final JobSkillMappingsService jobSkillMappingService;
    private final JobSkillsService jobSkillsService;

    public JobSkillMappingController(JobSkillMappingsService jobSkillMappingService, JobSkillsService jobSkillsService) {
        this.jobSkillMappingService = jobSkillMappingService;
        this.jobSkillsService = jobSkillsService;
    }

    /**
     * 分页查询
     */
    @GetMapping("/list")
    public Result<PageResponse<JobSkillMappings>> list(@RequestParam(defaultValue = "1") int page,
                                                       @RequestParam(defaultValue = "10") int size) {
        Page<JobSkillMappings> jobSkillMappingsPage = new Page<>(page, size);
        Page<JobSkillMappings> resultPage = jobSkillMappingService.page(jobSkillMappingsPage);
        List<JobSkillMappings> records = resultPage.getRecords();

        log.info("查询结果: {}", records);

        PageResponse<JobSkillMappings> response = new PageResponse<>();
        response.setTotal(resultPage.getTotal());
        response.setList(records);

        return Result.success(response);
    }

    /**
     * 根据 jobId 查询
     */
    @PostMapping("/query")
    public Result<List<JobSkillMappings>> query(@RequestBody JobSkillMappingUpdateRequest request) {
        QueryWrapper<JobSkillMappings> queryWrapper = new QueryWrapper<>();
        if (request.getJobId() != null) {
            queryWrapper.eq("job_id", request.getJobId());
        }
        if (request.getSkillId() != null) {
            queryWrapper.eq("skill_id", request.getSkillId());
        }

        List<JobSkillMappings> mappings = jobSkillMappingService.list(queryWrapper);
        return Result.success(mappings);
    }

    /**
     * 新增技能映射
     */
    @PostMapping("/add")
    public Result<String> add(@RequestBody JobSkillMappings jobSkillMappings) {
        jobSkillMappingService.save(jobSkillMappings);
        return Result.success("添加成功");
    }

    /**
     * 更新技能映射
     */
    @PostMapping("/update")
    public Result<String> update(@RequestBody JobSkillMappingUpdateRequest request) {
        JobSkillMappings mapping = BeanUtil.copyProperties(request, JobSkillMappings.class);
        jobSkillMappingService.updateById(mapping);
        return Result.success();
    }

    /**
     * 删除技能映射
     */
    @PostMapping("/delete")
    public Result<String> delete(@RequestBody JobSkillMappings jobSkillMappings) {
        jobSkillMappingService.removeById(jobSkillMappings.getJobId());
        return Result.success("删除成功");
    }

    /**
     * 根据 jobId 获取详情
     */
    @GetMapping("/getById")
    public Result<JobSkillMappings> getById(@RequestParam Integer jobId) {
        JobSkillMappings mapping = jobSkillMappingService.getById(jobId);
        return Result.success(mapping);
    }
//    通过职位id查询所有技能
    @PostMapping("/getSkillIdsByJobId")
    public Result<Map<Integer,String>> getSkillIdsByJobId(@RequestBody Jobs jobs) {
        QueryWrapper<JobSkillMappings> queryWrapper = new QueryWrapper<>();
        if (jobs.getJobId() != null) {
            queryWrapper.eq("job_id", jobs.getJobId());
        }

        //根据JobSkillMappings中查询出的jobId对应skillId,去查询skillName
        List<JobSkillMappings> mappings = jobSkillMappingService.list(queryWrapper);
        List<Integer> skillIds = new ArrayList<>();
        for (JobSkillMappings mapping : mappings) {
            skillIds.add(mapping.getSkillId());
        }
        //检查id数组不为空
        if (skillIds.isEmpty()) {
            return Result.failure(400,"未获取到技能");
        }
        // 根据skillId查询在jobSkills中的skillName
        List<String> skillNames = jobSkillsService.listByIds(skillIds).stream().map(JobSkills::getSkillName).toList();
        Map<Integer, String> skillNameMap = new HashMap<>();
        for (int i = 0; i < skillNames.size(); i++) {
            skillNameMap.put(i, skillNames.get(i));
        }
        return Result.success(skillNameMap);
    }
}
